namespace Microsoft.ServiceModel.Samples.Discovery
{
    using System;
    using System.Collections.Generic;
    using Microsoft.ServiceModel.Samples;
    using Microsoft.ServiceModel.Samples.Discovery.Protocol;

    internal class FindAsyncResult : AsyncResult
    {
        FindContext context;

        public FindAsyncResult(FindContext context, AsyncCallback callback, 
            object state)
            : base(callback, state)
        {
            this.context = context;
            this.context.AsyncResult = this;
        }

        public void Complete(Exception error)
        {
            base.Complete(false, error);
        }

        public void End()
        {
            AsyncResult.End<FindAsyncResult>(this);
        }

        public static FindAsyncResult Validate(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
                throw new ArgumentNullException("asyncResult");

            FindAsyncResult findAsyncResult = asyncResult as FindAsyncResult;
            if (findAsyncResult == null)
                throw new ArgumentException(ErrorMessages.InvalidAsyncResult, 
                        "asyncResult");

            return findAsyncResult;
        }

        public FindContext Context
        {
            get
            {
                return this.context;
            }
        }
    }
}
